package com.bien.springboot.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Slf4j
@WebFilter(filterName = "AnnotationFilter", urlPatterns = {"/*"})
@Order(1)
public class AnnotationFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("AnnotationFilter - {}初始化注解实现的过滤器...", new Date());
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        log.info("AnnotationFilter - Request URL: {}", request.getRequestURL().toString());
        log.info("AnnotationFilter - Request port：{}", request.getServerPort());
        log.info("AnnotationFilter - Request Method: {}", request.getMethod());
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Current-Path", request.getServletPath());
        response.setHeader("My-Name", "MeiNanzi");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        log.info("AnnotationFilter - 注解实现的过滤器已销毁...");
    }
}
